#!/bin/sh
set -ex
go_version="1.17.3"
go version

APP_NAME="bird"
rm -rf output
mkdir output

# 守护进程设置执行权限
chmod 755 supervise.${APP_NAME}
chmod 775 control.sh

# 编译
go mod tidy
go build -o ${APP_NAME} main.go

# 编译失败以非0 code 退出脚本
if [[ $? != 0 ]];then
    echo "执行编译失败"
    exit $?
fi
echo "编译成功"

rm -rf output
mkdir -p output

# 打包
cp -rf ./config ./output
cp -rf ./assets ./output
cp -rf ./docs ./output
cp -rf ./control.sh ./output
cp -rf ${APP_NAME} ./output
cp -rf ./internal/pkg/code/code.go ./output
cp -rf ./supervise.${APP_NAME} ./output

cd output

# 清理不需要的文件
find ./ -type d -name .git |xargs -i rm -rf {}
rm ./config/*.go

tar -zcf ../${APP_NAME}.tar.gz ./*

cd ..
rm -rf output/


